//src/utils/net.js
// import wx from 'wx';//引用微信小程序wx对象
import tip from './tip';
import base_util from './base_util';

let api_util = {};

//测试环境
// 张婷
// const _baseUrl = 'http://192.168.38.101:8080';
// 淘红波
// const _baseUrl = 'http://192.168.38.200:9001/shuliang-app-api';
//体验版
// const _baseUrl = '';
//正式环境
const _baseUrl = 'https://www.shuliangst.com/shuliang-app-api';
//测试环境
// const _baseUrl = 'http://boven.ittun.com/shuliang-app-api';

api_util.urls = {
    //baseUrl
    baseUrl: _baseUrl,

    imgUrl:"https://www.shuliangst.com/mall_file",
    /**
     * 用户模块
     * **/

    //文件上传(Post,参数:mobile,type)
    // fileUpload: 'http://192.168.38.140:8765/fileManage/file/upload',
    fileUpload: 'https://www.shuliangst.com/fileManage/file/upload',
    //发送短信验证码(Post,参数:mobile,type)
    sendVerifyCode: _baseUrl + '/common/sendVerifyCode',
    //登录(Post,参数:mobile,code)
    userLogin: _baseUrl + '/login',
    //更换手机号(Post,参数:originMobile,mobile,code,v)
    modifyMobile: _baseUrl + '/members/modifyMobile',
    //获取用户基本信息(Get,参数:mobile,code)
    userInfo: _baseUrl + '/members/baseInfo',
    //用户反馈信息(post,参数:content,image,contact,category)
    feedbackCreate: _baseUrl + '/feedback/create',
    //修改用户信息(post,参数:email,nickname,headimg,birthday,gender)
    updateInfo: _baseUrl + '/members/modifyBaseInfo',

    //通过手机号查询该用户是否和微信小程序绑定(v,mobile)
    checkWxApplet: _baseUrl + '/members/checkWxApplet',
    //绑定微信小程序(v,jsCode)
    bindWxApplet: _baseUrl + '/members/bindWxApplet',
    /**
     * 商品模块
     * **/

    //商品分类列表(get,参数:pageSize,pageNumber,parentId,depth)
    goodsCategoryList: _baseUrl + '/goods/category/list',
    //商品列表(Get,参数:keyword,type,orderField,orderBy,pageNumber,pageSize)
    goodsList: _baseUrl + '/goods/list',
    //商品详情(Get,参数:id)
    goosDetail: _baseUrl + '/goods/getDetailById',

    /**
     * 收货地址
     * **/

    //修改用户收地(post,参数:id,v,name,mobile,address,provinceId,cityId,districtId)
    updataAddress: _baseUrl + '/userAddress/modify',
    //添加用户地址(post,参数:id,v,name,mobile,address,provinceId,cityId,districtId)
    createAddress: _baseUrl + '/userAddress/create',
    //用户地址列表(get,参数:v)
    addressList: _baseUrl + '/userAddress/list',
    //删除地址(post,参数:v,id)
    deleteAddress: _baseUrl + '/userAddress/delete',
    //用户收货地址详情(GET,参数:v,id)
    detailsAddress: _baseUrl + '/userAddress/details',
    //设为默认地址(POST,参数:v,id)
    modifyAcquiescence: _baseUrl + '/userAddress/modifyAcquiescence',

    /**
     * 订单模块
     * **/

    //加入购物车(post,参数:v,goodsId,storeId,number,price,goodsAttrIds_选填_加购商品属性集合（颜色，尺寸）)
    addShoppingCart: _baseUrl + '/shoppingCart/add',
    //编辑购物车(post,参数:v,id,number)
    editShoppingCart: _baseUrl + '/shoppingCart/edit',
    //购物车列表(get,参数:pageSize,pageNumber)
    listShoppingCart: _baseUrl + '/shoppingCart/list',
    //订单详情(get,参数:id)
    orderDetail: _baseUrl + '/order/detail',
    //取消订单(post,参数:orderId)
    cancelOrder: _baseUrl + '/order/cancel',
    //确认收货(post,参数:orderId)
    confirmGoods: _baseUrl + '/order/updateOrderStatus',
    //删除购物车(post,参数:v,id)
    deleteShoppingCart: _baseUrl + '/shoppingCart/delete',
    //用户申请补发(post,参数:orderId,number,reason,remark,pics)
    orderApply: _baseUrl + '/order/apply',
    //订单列表(get,参数:status,pageNumber,pageSize)
    orderList: _baseUrl + '/order/list',
    //用户申请补发(post,参数:number,orderId,reason,remark,pics)
    ordeapply: _baseUrl + '/order/apply',
    //创建订单(post,参数:v,goodsId,cartIds,quantity,remark,shippingAddressId,deduction,integrals)
    creatOrder: _baseUrl + '/order/create',

    /**
     * 文章
     * **/

    //文章列表(get,参数:categoryId,position,pageNumber,pageSize)
    informationList: _baseUrl + '/information/list',
    //文章详情(get,参数:id)
    informationDetail: _baseUrl + '/information/details',
    //banner(get,参数:position_0.app首页广告)
    bannerList: _baseUrl + '/ad',

    /**
     *获取小程序相关信息
     */

    //获取微信小程序二维码(GET,参数:v_1.0.0,page)
    wxCode: _baseUrl + '/wx/appletCode',

    /**
     *积分
     */

    //查看积分(GET,参数:v_1.0.0)
    myAccount: _baseUrl + '/member/integral/myAccount',
    //查询我和团队的收益积分(GET,参数:v_1.0.0)
    earningAccount: _baseUrl + '/member/integral/earning',
    //查询我的团队积分列表(GET,参数:v_1.0.0,pageNumber,pageSize,orderby)
    teamList: _baseUrl + '/member/integral/teamList',
    //查询我的积分记录（列表）(GET,参数:v_1.0.0,pageNumber,pageSize,orderby,kind)
    integralList: _baseUrl + '/member/integral/list',

    /**
     *评论
     */

    //查询商品平均评分，好/中/差评数量(GET,参数:v_1.0.0,goodId)
    scoreByGood: _baseUrl + '/goodsComment/scoreByGood',
    //订单是否能提交评论(GET,参数:orderId)
    getApply: _baseUrl + '/order/getApply',
    //查询商品评论（只查首评）(GET,参数:v_1.0.0,pageNumber,pageSize,orderby,goodId,level)
    listByGood: _baseUrl + '/goodsComment/listByGood',
    //查询评论详情(GET,参数:v_1.0.0,oid_评论id)
    detailComment: _baseUrl + '/goodsComment/detail',
    //添加商品评论/追评（列表）(GET,参数:v_1.0.0,orderId,goodId,anonymity,goodsScore,logisticsScore,serviceScore,content,images)
    createComment: _baseUrl + '/goodsComment/create',
    //通过订单id 和商品id查询评论；（用于 用户订单中心，查看自己的评论；要用户登陆；只会查出首评，不会查出追评）(GET,参数:v_1.0.0,pageNumber,pageSize,orderby,goodId)
    myOrderComment: _baseUrl + '/goodsComment/myOrderComment',

    /**
     *用户消息
     */

    //我的消息列表(GET,参数:v_1.0.0,pageNumber,pageSize,type,read)
    msgList: _baseUrl + '/message/list',
    //未读消息条数(GET,参数:v_1.0.0)
    msgUnreadCount: _baseUrl + '/message/unreadCount',
    //详情（如果消息未读，把消息标记为已读）(GET,参数:v_1.0.0,oid)
    msgDetail: _baseUrl + '/message/detail',
    //阅读消息（将消息标为已读；不调该接口，详情接口也可以标记已读）(POST,参数:v_1.0.0,oid)
    messageRead: _baseUrl + '/message/read',


    /**
     * 支付
     */

    //微信支付(post,参数:status,price,integral,payChannels,orderId,nonceStr,signature)
    wxPay: _baseUrl + '/order/orderPay_v1_1',
    //微信的虚拟回调(post,参数:orderId)
    weixinCallbacktest: _baseUrl + '/orderPay/weixinCallbacktest',
    //支付结果查询(get,参数:orderId)
    payResult: _baseUrl + '/order/payResult',

}
api_util.wxRequest = {
    apiRequest(vm, url, params) {
        base_util.loading();
        wx.request({
            url: url,
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                'token': base_util.getStorageSync('token') ? base_util.getStorageSync('token') : '',
            },
            method: params.method || 'GET',
            data: params.data || {},
            success: function (response) {
                if (params.success && response.data) {
                    if (response.data.code == '0199999') {
                        params.success(response.data);
                        if (params.headerInfo) {
                            params.headerInfo(response.header);
                        }
                    } else {

                        // if (response.data.code == '0101001' || response.data.code == '0101007' || response.data.code == '0101006') {} else {
                        setTimeout(function () {
                            tip.alert(response.data.message);
                        }, 100);
                        // }

                        console.log('code:', response.data.code);
                        params.code && params.code(response.data);
                    }
                }
            },
            fail: function (error) {
                setTimeout(function () {
                    tip.alert(response.data.message | response.data.message);
                }, 100)
                params.error && params.error(error);
                // tip.loaded();
            },
            complete: function (response) {
                params.complete && params.complete(response);
                base_util.loaded();
            }
        });
    }
}

function requestHeader() {
    let header = {};
    return header;
}

export default api_util
